<?php

/**
 * anulacion_tesis actions.
 *
 * @package    sistema_estadisticas
 * @subpackage anulacion_tesis
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class anulacion_tesisActions extends sgeActions
{

	public function execute($request)
	{
		$tesis_id = $request->getUrlParameter('tesis_id');
		$this->forward404Unless($tesis_id);

		$this->getContext()->getRouting()->setDefaultParameter('tesis_id', $tesis_id);

		return parent::execute($request);
	}

	public function executeIndex(sfWebRequest $request)
	{
		$anulacion_id = AnulacionTesisTable::getInstance()->getAnulacionId($request->getParameter('tesis_id'));

		if ($anulacion_id)
		{
			$this->redirect('@anulacion_tesis_show?id=' . $anulacion_id);
		}
		else
		{
			$this->redirect('@anulacion_tesis_new');
		}
	}

	public function executeShow(sfWebRequest $request)
	{
		$id = $request->getParameter('id');
		$this->anulacion = AnulacionTesisTable::getInstance()->find($id);
		$this->forward404Unless($this->anulacion, 'La anulación no existe');
	}

	public function executeNew(sfWebRequest $request)
	{
		$this->tesis_id = $request->getUrlParameter('tesis_id');
		$anulacion_id = AnulacionTesisTable::getInstance()->getAnulacionId($this->tesis_id);

		if ($anulacion_id)
		{
			$this->redirect('@anulacion_tesis_show?id=' . $anulacion_id);
		}
		else
		{
			$this->forward404Unless(TesisTable::getInstance()->existeTesis($this->tesis_id), 'El proyecto de tesis no existe');

			$anulacion_tesis = new AnulacionTesis();
			$anulacion_tesis->setTesisId($this->tesis_id);
			$this->form = new AnulacionTesisForm($anulacion_tesis);
		}
	}

	public function executeCreate(sfWebRequest $request)
	{
		$this->tesis_id = $request->getUrlParameter('tesis_id');
		$this->form = new AnulacionTesisForm();

		$this->processForm($request, $this->form);

		$this->form->getObject()->setTesisId($request->getUrlParameter('tesis_id'));
		$this->setTemplate('new');
	}

	public function executeEdit(sfWebRequest $request)
	{
		$this->tesis_id = $request->getUrlParameter('tesis_id');

		$anulacion = AnulacionTesisTable::getInstance()->find($request->getParameter('id'));
		$this->forward404Unless($anulacion && $anulacion->tesis_id == $this->tesis_id, 'La anulación no existe');
		$this->form = new AnulacionTesisForm($anulacion);
		$this->setTemplate('edit');
	}

	public function executeUpdate(sfWebRequest $request)
	{
		$this->tesis_id = $request->getUrlParameter('tesis_id');
		$this->anulacion = $this->getRoute()->getObject();
		$this->form = new AnulacionTesisForm($this->anulacion);

		$this->processForm($request, $this->form);

		$this->setTemplate('edit');
	}

	protected function processForm(sfWebRequest $request, sfForm $form)
	{
		$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

		if ($form->isValid())
		{
			$notice = 'La anulación se ha guardado correctamente.';

			try
			{
				$anulacion_tesis = $form->save();
			}
			catch (Doctrine_Validator_Exception $e)
			{
				$this->getUser()->setFlash('error', 'La anulación no se ha guardado porque se ha producido algún error.');
				return sfView::SUCCESS;
			}

			$this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $anulacion_tesis)));

			$this->getUser()->setFlash('notice', $notice);
			$this->redirect('@anulacion_tesis_show?id=' . $anulacion_tesis->getId());
		}
		else
		{
			$this->getUser()->setFlash('error', 'La anulación no se ha guardado porque se ha producido algún error.', false);
		}
	}

}
